﻿
namespace TFSChangeLog.Operations.Command
{
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Input;
    using TFSChangeLog.Operations.Services;
    using TFSChangeLog.ViewModel;

    /// <summary>Summary description for <see cref="ShowSelectedProjectBranchNodeCommand"/> class.</summary>
    public class ShowSelectedProjectBranchNodeCommand
    {
        #region Fields

        /// <summary>Summary description for class level field of type <c>TFSChangeLog.ViewModel.MainWindowViewModel</c>.</summary>
        private MainWindowViewModel m_ViewModel;

        /// <summary>Gets the value for ShowSelectedProjectBranchNode.</summary>
        public ICommand ShowSelectedProjectBranchNode { get; private set; }

        #endregion

        #region Constructor

        /// <summary>Initializes a new instance of the <see cref="ShowSelectedProjectBranchNodeCommand"/> class.</summary>
        /// <param name="viewModel">TODO: Summary description for <c>viewModel</c> parameter of type <c>TFSChangeLog.ViewModel.MainWindowViewModel</c>.</param>
        public ShowSelectedProjectBranchNodeCommand(MainWindowViewModel viewModel)
        {
            this.m_ViewModel = viewModel;
            this.GenerateCommand();
        }

        /// <summary>Summary description for <c>ShowSelectedProjectBranchNodeCommand</c> method.</summary>
        private void GenerateCommand()
        {
            this.ShowSelectedProjectBranchNode = new RelayCommand(this.OnShowSelectedProjectBranchNode, this.CanSelectProjectBranchNode);
        }
        #endregion

        /// <summary>Summary description for <c>ShowSelectedProjectBranchNodeCommand</c> method.</summary>
        /// <param name="o">TODO: Summary description for <c>o</c> parameter of type <c>object</c>.</param>
        /// <returns>TODO: returns an object of type <c>bool</c>.</returns>
        private bool CanSelectProjectBranchNode(object o)
        {
            return !(this.m_ViewModel.FirstGeneration == null || this.m_ViewModel.FirstGeneration.Count == 0);
        }

        /// <summary>Summary description for <c>ShowSelectedProjectBranchNodeCommand</c> method.</summary>
        /// <param name="context">TODO: Summary description for <c>context</c> parameter of type <c>object</c>.</param>
        private void OnShowSelectedProjectBranchNode(object context)
        {
            var progressDialogViewModel = this.m_ViewModel.progressDialogViewModel;
            int[] singleIterationList = { 0 };
            progressDialogViewModel.ResetViewModel();

            progressDialogViewModel.TokenSource = new CancellationTokenSource();

            // Announce that work is starting
            this.m_ViewModel.RaiseProjectBranchNodeWorkStartedEvent();

                Task selectBranchNodeTask = Task.Factory.StartNew(
                    () => SelectBranchNodeService.DoWork(
                    singleIterationList, this.m_ViewModel), progressDialogViewModel.TokenSource.Token);

            // Announce that work is ending
            selectBranchNodeTask.ContinueWith(
                                      t => this.m_ViewModel.RaiseProjectBranchNodeWorkEndedEvent(),
                                      TaskScheduler.FromCurrentSynchronizationContext());
        }
    }
}
